<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Auth;

class MailboxController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询用户游戏数据
        $mailbox = DB::table('mailbox')
            ->select('id', 'title', 'content', 'sender', 'recipient', 'create_date', 'read')
            ->where('recipient', Auth::user()->username)
            ->orderBy('id', 'desc')
            ->paginate(17);

        return view('admin.mailbox.index',['mailbox'=>$mailbox]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.mailbox.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //验证表单
        $this->validate($request, [
            'email' => ['required','email','max:20'],
            'title' => ['required','string','max:150'],
            'contents' => ['required','string','max:500'],
        ]);

        $user = DB::table('users')
            ->where('email', $request->email)
            ->exists();

        if(!$user){
            return response()->json(['message' => 'The given data was invalid.', 'errors' => ['email' => ['收件人不存在。']]], 422);
        }

        if( $request->email == Auth::user()->username){
            return response()->json(['message' => 'The given data was invalid.', 'errors' => ['email' => ['不能写信给自己。']]], 422);
        }

        //写入添加的游戏
        DB::table('mailbox')->insert([
            'title' => $request->title,
            'content' => $request->contents,
            'sender' => Auth::user()->username,
            'recipient' => $request->email,
            'read' => 0,
            'create_date' => date('Y-m-d H:i:s')
        ]);

        return response()->json(['message' => '发送成功。', 'success' => 'true']);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        //修改为已读
        DB::table('mailbox')
            ->where('id', $id)
            ->update(['read' => '1']);

        //查询站内信第一封数据
        $mailbox = DB::table('mailbox')
            ->select('id', 'title', 'content', 'sender', 'recipient', 'create_date', 'read')
            ->where('id', $id)
            ->first();

        return view('admin.mailbox.show',['mailbox'=>$mailbox]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        DB::table('mailbox')
            ->whereIn('id', explode(',', $id))
            ->delete();

        return response()->json(['message' => '删除成功！', 'success' => 'true']);
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function getMailbox()
    {
        //查询未读数量
        $mailbox_count = DB::table('mailbox')
            ->select('id', 'title', 'content', 'sender', 'recipient', 'create_date', 'read')
            ->where('recipient',  Auth::user()->username)
            ->where('read','0')
            ->orderBy('id', 'desc')
            ->count();

        //查询与第一条关联的信息
        $mailbox = DB::table('mailbox')
            ->select('id', 'title', 'content', 'sender', 'recipient', 'create_date', 'read')
            ->where('recipient',  Auth::user()->username)
            ->where('read','0')
            ->orderBy('id', 'desc')
            ->limit(3)
            ->get();

        return response()->json([$mailbox_count,$mailbox]);
    }
}
